--
-- DESCRIPTION
--
-- @COMPANY **
-- @AUTHOR **
-- @DATE ${date} ${time}
--
--require("UnLuaDebug")

---@type BP_Captain_C
local M = UnLua.Class("Action.Blueprints.Characters.BP_ActionChar")

local KismetSystemLibrary = UE.UKismetSystemLibrary

-- function M:Initialize(Initializer)
-- end

-- function M:UserConstructionScript()
-- end

--function M:ReceiveBeginPlay()
--    self.Super.ReceiveBeginPlay(self)
--end

-- function M:ReceiveEndPlay()
-- end

--- 初始化定时器 self.Super.调用父类
function M:InitializeTimer()
    self.Super.InitializeTimer(self)
    -- 跳跃缓存定时器
    self.JumpBuffering = UE.FTimerHandle()
    -- 检测平台定时器
    self.CanTestPlatformTimer = UE.FTimerHandle()
end
--- 初始化绑定相关
function M:InitializingBinding()
    self.Super.InitializingBinding(self)
    self.Defeated:Add(self, self.FunDefeated)
end

function M:ReceiveTick(DeltaSeconds)
    --  self.Super.ReceiveTick(self) -- 父类用到了再加

    if not self.IsDefeated then
        self:TraceForPlatforms()

        -- 判断是否有跳跃的按键缓存
        if self.IsJumpBuffering and self:CanJump() and self:GetVelocity().Z <= 0 then
            self:Jump()
        end
    end

end

-- function M:ReceiveAnyDamage(Damage, DamageType, InstigatedBy, DamageCauser)
-- end

-- function M:ReceiveActorBeginOverlap(OtherActor)
-- end

-- function M:ReceiveActorEndOverlap(OtherActor)
-- end

function M:JumpPressed()

    --- 不可以行动
    if not self:CanAct() then
        print("不可以跳跃")
        return
    end

    self:SetCapsuleOverlap()
    self.CanTestPlatform = false

    KismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.CanTestPlatformTimer)
    self.CanTestPlatformTimer = KismetSystemLibrary.K2_SetTimerDelegate({ self, self.CanPlatform }, 0.15, false)

    if self:CanJump() then
        self:Jump()
    else
        self:SetIsJumpBufferingStart()
    end
end

function M:JumpReleased()
    self:StopJumping()
end

--- 当角色离开表面并即将下落时触发事件
function M:OnWalkingOffLedge(PreviousFloorImpactNormal, PreviousFloorContactNormal, PreviousLocation, TimeDelta)
    if not self.WalkingOffLedge then
        self.JumpMaxCount = self.JumpMaxCount + 1
    end
    self.WalkingOffLedge = true
    --  print("走出平台了  跳跃次数：" .. self.JumpMaxCount)
end

--- 可以检测平台
function M:CanPlatform()
    self.CanTestPlatform = true
end

--- 落地
---@param Hit FHitResult
function M:OnLanded(Hit)
    --- 落地播放声音
    local LandedSound = UE.UObject.Load("/ActionPlatformer/Sounds/Footsteps/S_Step_R_Cue.S_Step_R_Cue")
    UE.UGameplayStatics.PlaySoundAtLocation(self, LandedSound, self:K2_GetActorLocation())

    --- 走出平台判断
    if self.WalkingOffLedge then
        self.JumpMaxCount = self.JumpMaxCount - 1
        self.WalkingOffLedge = false
    end
end

--- 设置跳跃缓冲开始
function M:SetIsJumpBufferingStart()
    --- 把跳跃储存在缓冲里面，一定时间内会自动触发跳跃
    self.IsJumpBuffering = true
    --[[
        按下跳跃的时候清除上一次创建的Delegate(代理/委托)
        实现效果始终在一定时间内只记录一次缓冲操作
    ]]
    KismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.JumpBuffering)
    self.JumpBuffering = KismetSystemLibrary.K2_SetTimerDelegate({ self, self.SetIsJumpBufferingEnd }, self.JumpBufferLength, false)
end

-- 设置跳跃缓冲结束
function M:SetIsJumpBufferingEnd()
    self.IsJumpBuffering = false
    KismetSystemLibrary.K2_ClearAndInvalidateTimerHandle(self, self.JumpBuffering)
    -- print("清除跳跃缓存定时器")
end

--  更新控制器旋转
function M:UpdateControllerRotation(Value2D)

    if self.PlayerController == nil then
        return
    end
    -- print("设置旋转")

    --[[
    判断速度方向，来进行旋转
    在空中也可以根据按键传入的方向来做旋转
    ]]
    if (self.CharacterMovement.Velocity.X > 0 and Value2D.X > 0) or (self.CharacterMovement:IsFalling() and Value2D.X ~= 0 and Value2D.X > 0) then
        self.PlayerController:SetControlRotation(UE.FRotator(0, 0, 0))
    end
    if (self.CharacterMovement.Velocity.X < 0 and Value2D.X < 0) or (self.CharacterMovement:IsFalling() and Value2D.X ~= 0 and Value2D.X < 0) then
        self.PlayerController:SetControlRotation(UE.FRotator(0, 180, 0))
    end
end

function M:Move(Value)
    if not self:CanAct() then
        return
    end
    self.MoveVector = UE.UEnhancedInputLibrary.Conv_InputActionValueToAxis2D(Value)
    -- print("移动" .. tostring(self.MoveVector))
    self:AddMovementInput(UE.FVector(1, 0, 0), self.MoveVector.X, false)
    self:AddMovementInput(UE.FVector(0, -1, 0), self.MoveVector.Y, false)
    self:UpdateControllerRotation(self.MoveVector)
end

function M:MoveCompleted(Value)
    self.MoveVector = UE.UEnhancedInputLibrary.Conv_InputActionValueToAxis2D(Value)
    -- print("停止移动" .. tostring(self.MoveVector))
end

--- 攻击按下
function M:AttackPressed()
    if not self:CanAct() then
        return
    end
    -- print(self.GetVelocity(self))
    self:Attack()
end

--- 扔出按下
function M:ThrowPressed()
    if not self:CanAct() then
        return
    end
    if self:AbilityCheck(UE.EAbility.EA_SwordThrow) then
        self:Throw()
    else
        print("没有获得投掷能力！！！")
    end

end

--- 被击败函数
function M:FunDefeated()
    self:UnBindingInput() -- 解除增强输入绑定
    KismetSystemLibrary.K2_SetTimerDelegate({ self, self.RestartGame }, 3, false)
end

--- 重新开始游戏
function M:RestartGame()
    self.GameMode:RestartGame()
end

return M